package routers

import (
	"github.com/gin-gonic/gin"

	"hytx_media/middleware/jwt"
	"hytx_media/pkg/setting"
	"hytx_media/routers/api"
	"hytx_media/routers/api/v1"
	"net/http"
	"hytx_media/models"
	"github.com/gin-contrib/gzip"
	"hytx_media/pkg/upload"
	"hytx_media/middleware/cros"
)

func InitRouter() *gin.Engine {
	r := gin.New()
	// gzip 压缩
	r.Use(gzip.Gzip(gzip.DefaultCompression))

	r.Use(gin.Logger())

	r.Use(gin.Recovery())
	gin.SetMode(setting.ServerSetting.RunMode)

	// 跨域
	r.Use(cros.Cors())

	// 静态资源路径
	r.StaticFS("/upload/images", http.Dir(upload.GetRouterImageFullPath()))

	//root组下接口
	root := r.Group("/api/v1")
	//百度编辑器后端支持
	root.Any("ueditor", v1.UEditor)

	root.GET("/ip", v1.IP)                          // ip
	root.GET("/captcha", v1.GenerateCaptchaHandler) // 验证码id
	root.POST("/captcha/verfiy", v1.VerfiyCaptcha)  // 验证验证码

	root.GET("/sms/test", v1.Test)

	// 发送手机短信验证码
	root.GET("/sms/sendVerification", v1.SMSsendVerification)
	root.POST("/upload", api.UploadImage)

	// 登录授权获取token
	root.POST("/auth", api.GetAuth)
	// 个人注册
	root.POST("/register", v1.Register)

	// 重置密码
	root.POST("/reset_password", v1.ResetPassword)

	// 查看文章分类

	root.GET("/article_category", v1.GetArticleCategory)

	// 授权 需要授权的接口使用auth组
	auth := root.Group("/")
	auth.Use(jwt.JWT())
	{
		auth.GET("/test", func(context *gin.Context) {
			user := context.MustGet("user").(*models.MediaUser)
			context.String(http.StatusOK, user.Phone)
		})
		auth.GET("/articles/:id/comments", v1.GetComments)
		//置顶
		auth.POST("/articles/:id/comments/:cid/stick", v1.CommentStick)
		auth.GET("/comments", v1.GetCommentNumList)
		auth.POST("/comments/:id/feedback", v1.CommentFeedback)
		auth.GET("/message/num", v1.GetMessageNum)
		auth.GET("/message", v1.GetMessage)
		auth.GET("/message/content/:id", v1.GetNoticeContent)

		// 文章
		auth.GET("/article", v1.GetArticles)              // 获取文章列表
		auth.POST("/article", v1.AddArticle)              // 新增文章列表
		auth.GET("/article/:id", v1.GetArticle)           // 查看单个文章列表
		auth.PUT("/article/:id", v1.EditArticle)          // 修改文章接口
		auth.DELETE("/article/:id", v1.DeleteArticle)     // 删除文章
		auth.PUT("/article_revoke/:id", v1.RevokeArticle) // 撤回文章
		// 文章标签
		auth.GET("/article_label", v1.GetArticleLabels)       // 获取文章标签
		auth.GET("/article_opera_num", v1.SumArticleOperaNum) // 统计文章操作数量

		// 注册-选择主题-个人
		auth.POST("/register_personal", v1.RegisterPersonal)
		// 注册-选择主题-新闻媒体
		auth.POST("/register_new_media", v1.RegisterNewMedia)

		auth.GET("/register_personal_list", v1.RegisterPersonalList)

		// 修改-选择主题-个人
		auth.POST("/register_personal_update", v1.RegisterPersonalUpdate)
		// 修改-选择主题-新闻媒体
		auth.POST("/register_new_media_update", v1.RegisterNewMediaUpdate)

		// 修改密码
		auth.POST("/modify_password", v1.ModifyPassword)
		auth.GET("/profile", v1.Profile)
		//首页
		indexGroup := auth.Group("/index")
		{
			indexGroup.GET("/quantity", v1.Quantity)
			indexGroup.GET("/notice", v1.Notice)
			indexGroup.GET("/slides", v1.Slides)
			indexGroup.GET("/articles", v1.NewArticles)
		}

		//用户设置
		settingGroup := auth.Group("/setting")
		{
			settingGroup.POST("/", v1.Setting)
			settingGroup.GET("/profile", v1.SettingProfile)
			//settingGroup.POST("/:name", v1.Setting)
			settingGroup.GET("/bind", v1.BindInfo)
			settingGroup.POST("/validate/phone", v1.ValidateOldPhone)
			settingGroup.POST("/bind/phone", v1.BindPhone)
			settingGroup.POST("/watermark", v1.SetWatermark)
			settingGroup.GET("/jurisdiction", v1.Jurisdiction)
		}
	}
	return r
}
